package com.atguigu.guli.api.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class GatewayConfig {

    //CorsWebFilter:网关项目提供的用来配置跨域的全局过滤器

    @Bean
    public CorsWebFilter corsWebFilter(){

        UrlBasedCorsConfigurationSource corsConfigurationSource=new UrlBasedCorsConfigurationSource();
        //参数1：需要进行跨域配置的路径  参数2：路径对应的跨域配置

        CorsConfiguration config=new CorsConfiguration();
        config.setAllowCredentials(true);//是否允许请求携带cookie
        config.addAllowedOrigin("*");//允许那些服务器跨域访问  *代表所以
        config.addAllowedMethod("*");//允许的请求方式
        config.addAllowedHeader("*");//允许携带任意的请求头放法
        corsConfigurationSource.registerCorsConfiguration("/**",config);
        return new CorsWebFilter(corsConfigurationSource);
    }

}
